﻿@using ApproveFlowEngine.Approve
@using ApproveFlowEngine.Approve.DTO
@using ApproveFlowEngine.Enums.Core
@inject IApproveAppService _iApproveAppService


@* <Button Type="@ButtonType.Primary" @onclick="open">Setting</Button> *@
<Drawer Closable="true" Width="720" Visible="visible" Title='("Setting")' OnClose="_=>close()">
    <Template style="height:90%">
        <Row Gutter="16">
            <AntDesign.Col>
                <Text>Name</Text>
                <Input Placeholder="Please enter flow name" @bind-Value="@WorkFlow.FlowName" TValue="string"></Input>
            </AntDesign.Col>
        </Row>
        <br />
        <Row Gutter="16">
            <AntDesign.Col>
                <Text>FlowNo</Text>
                <Input Disabled Placeholder="Please enter flow no" @bind-Value="@WorkFlow.FlowNo" TValue="long" />
            </AntDesign.Col>
        </Row>
        <br />
        <Row>
            <AntDesign.Col>
                <Text>ApproveType</Text>
                <Select DataSource="@_approveTypes"
                        @bind-Value="@WorkFlow.ApproveType"
                        DefaultValue="@ApproveType.General"
                        ValueProperty="c=>c.Key"
                        LabelProperty="c=>c.Value"
                        Style="width:120px">
                </Select>
            </AntDesign.Col>
        </Row>
        <br />
        <Row>
            <AntDesign.Col>
                <Text>Remark</Text>
                <InputTextArea Placeholder="Please enter flow no" @bind-Value="@WorkFlow.Remark" />
            </AntDesign.Col>
        </Row>
        <ApproveTree Nodes=@WorkFlow.NodeTree IsEdit="true" />
        <br />
        <Row>
            <AntDesign.Col Span="18">

            </AntDesign.Col>
            <AntDesign.Col Span="6">
                <Button Type="default" @onclick="@Cancel">Cancel</Button>
                <Button Type="primary" @onclick="@Submit">Submit</Button>
            </AntDesign.Col>
        </Row>
    </Template>
</Drawer>




@code {
    [Parameter]
    public EventCallback OnCallBack { get; set; }
    [Parameter]
    public bool visible { get; set; }

    [Parameter]
    public WorkFlowDto WorkFlow { get; set; } = new WorkFlowDto();

    private Dictionary<NodeType, string> _nodeTypes = new Dictionary<NodeType, string>();
    private Dictionary<ApproveType, string> _approveTypes = new Dictionary<ApproveType, string>();

    void Cancel()
    {
        WorkFlow = new WorkFlowDto();

    }
    async Task Submit()
    {
        await _iApproveAppService.Update(WorkFlow.Id,WorkFlow);
        visible = false;
        if (OnCallBack.HasDelegate)
        {
            await OnCallBack.InvokeAsync();
        }
    }

    protected override void OnInitialized()
    {                                   
        var types = Enum.GetValues<NodeType>();
        foreach (var item in types)
        {
            _nodeTypes.Add(item, item.ToString());
        }
        var approveTypes = Enum.GetValues<ApproveType>();
        foreach (var item in approveTypes)
        {
            _approveTypes.Add(item, item.ToString());
        }
    }

    void Callback(string[] keys)
    {
        Console.WriteLine(string.Join(',', keys));
    } 
    void open()
    {
        this.visible = true;
    }

    void close()
    {
        this.visible = false;
    }
}
